package primalgorithms.sortandsearch;

public class _02FirstBadVersion {
    public static int firstBadVersion(int n) {
        int start = 1,end = n;
        return firstBadVersion(start,end);
    }

    private static int firstBadVersion(int start, int end) {
        if(start >= end){
            return start;
        }
        //测试用例中会给出想加超过int值域的数字
        long mid = ((long) start + (long) end) >> 2;
        if (isBadVersion((int)mid)){
            //从mid的前面继续纠错
            end = (int) mid;
            return firstBadVersion(start,end);
        }else {
            start = (int) mid + 1;
            return firstBadVersion(start,end);
        }
    }

    private static boolean isBadVersion(int version){
        //该函数其实有问题
        //6是随便写的一个数，假设从第6个开始不合格
        return (version == 1702766719);
    }
}
